library(tidyverse)
library(readxl)
path <- "Excel/900-999/910/910 Increasing Numbers.xlsx"
input <- read_excel(path, range = "A1:A11")
test <- read_excel(path, range = "B1:B11")
split_inc <- function(s) {
n <- nchar(s)
if (n == 0) {
character()
}
pos <- 1
prev <- substr(s, pos, pos)
out <- prev
pos <- pos + 1
while (pos <= n) {
found <- FALSE
for (len in 1:(n - pos + 1)) {
cand <- substr(s, pos, pos + len - 1)
if (as.integer(cand) > as.integer(prev)) {
out <- c(out, cand)
prev <- cand
pos <- pos + len
found <- TRUE
break
}
}
if (!found) break
}
paste(out, collapse = ", ")
}
result = input %>%
mutate(`Answer Expected` = map_chr(`Digit Strings (Input)`, split_inc))
all.equal(result$`Answer Expected`, test$`Answer Expected`)
# > TRUEExcel BI - Excel Challenge 910

Challenge Description
π° The prompt in 910 Increasing Numbers.xlsx says: Pick up the first digit. From the remaining string, find the shortest substring that forms a number strictly greater than the previous number. Once you use a position, you canβt reuse that. Keep finding till you exhaust the string. If the remaining digits cannot form a number larger than the previous one, discard them. > > Ex. 90212148 => 9 < 021 < 214 (last digit 8 will have to be discarded). The goal is to split the digit string into a left-to-right sequence of strictly increasing numbers, always using the shortest valid next chunk.
Solutions
- Logic: Take the first digit as the first number.; Scan the remaining string from left to right.; Try the shortest possible next chunk first..
- Strengths: The interesting part is the greedy choice:
text always pick the shortest next substring that is still largerThat rule is what makes the parse deterministic. - Areas for Improvement: The supplied Python file reports three mismatches, so the workbook-aligned reference here is the R logic.
- Gem:
text always pick the shortest next substring that is still larger
import pandas as pd
path = "Excel/900-999/910/910 Increasing Numbers.xlsx"
input = pd.read_excel(path, usecols="A", nrows=11)
test = pd.read_excel(path, usecols="B", nrows=11)
def split_inc(s):
i, prev, out = 1, int(s[0]), [s[0]]
while i < len(s):
for j in range(i + 1, len(s) + 1):
cand = s[i:j]
if int(cand) > prev:
out.append(cand)
prev, i = int(cand), j
break
else:
break
return ", ".join(out)
input['Split Numbers'] = input['Digit Strings (Input)'].astype(str).apply(split_inc)
print(input['Split Numbers'] == test['Answer Expected'])
# 3 differentThe supplied Python file reports three mismatches, so the workbook-aligned reference here is the R logic.
Difficulty Level
Easy
Once the core pattern is recognized, the implementation is short and direct.